package com.adv.web.handle;

import java.awt.Color;
import java.util.Map;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;

/**
 * 消息处理器handle
 * @author liyarong
 *
 */
public class WeixinHandle implements WxMpMessageHandler{

	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
			Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager)
			throws WxErrorException {
		     WxMpXmlOutMessage mpxmlOutMsg = null;
	        if(wxMessage.getMsgType().equals(WxConsts.XML_MSG_TEXT)){
	        	mpxmlOutMsg = WxMpXmlOutMessage.TEXT().content("老铁你太厉害了").toUser(
	        			wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
	        }
	        else if (wxMessage.getMsgType().equals(WxConsts.XML_MSG_EVENT)) {
	        	if (wxMessage.getEvent().equals(WxConsts.EVT_SUBSCRIBE)) {
	        		mpxmlOutMsg = WxMpXmlOutMessage.TEXT().content("恭喜你关注成功").toUser(
	        				wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
				}else if(wxMessage.getEvent().equals(WxConsts.EVT_CLICK)){
					WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
					templateMessage.setTemplateId("8fZL0Jg_T23UBP6MRnoKvAtGh_QDlHICaFOiQINgXKU");
					templateMessage.setToUser(wxMessage.getFromUserName());
					templateMessage.setTopColor(Color.red.toString());
					templateMessage.getDatas().add(new WxMpTemplateData("1", "dddd", Color.blue.toString()));
					templateMessage.getDatas().add(new WxMpTemplateData("2", "ssss", Color.blue.toString()));
					wxMpService.templateSend(templateMessage);
					//mpxmlOutMsg = WxMpXmlOutMessage.TEXT().content("恭喜你关注成功").toUser(wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
				}
	        	
			}
	        
	        return mpxmlOutMsg;
	}

}
